package com.jch.demo.jdbctemplate.basics.repository;

import com.jch.demo.jdbctemplate.basics.module.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public User get(Integer id){
        String selectSql = "select id,username,age,sex,remark,create_time createTime,update_time updateTime from `user` where id = ?";
        RowMapper<User> userRowMapper = new BeanPropertyRowMapper<>(User.class); // 构建对象与数据行映射
        User user = jdbcTemplate.queryForObject(selectSql,userRowMapper,id);
        return user;
    }

}
